* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization
* Additional Information on Bracelet Retention and Correct Bracelet Hand Out
* **************************************************************************** *
/*
** Purpose:  Generate Table  :
Additional Information on Bracelet Retention and Correct Bracelet Hand Out

*/
* **************************************************************************** *
* **************************************************************************** *

	use  "${Replicate_SocialSignals_dtaInter}/Listing_Data.dta", clear


	** merge in start dates

	sort clinic
	merge m:m clinic using "$Replicate_SocialSignals_dtaInter/MasterData_LaunchDates.dta", keep(3)


	**keep if children were born shortly before or after launch
	gen     start = eligible  
	drop if dob_listing <start


	keep if type_listing         == 1
	drop if immunization_listing == 0 //exclude all children that do not have any immunization
	drop if clinic_attendlist    == 0 //exclude all children that do not attend bracelet clinics



	* combine the immunization information from the listing and follow-up,
	* and drop those with zero immunizations
	foreach    vaccine in bcg penta1 penta2 penta3 measles1 {
		gen     `vaccine'_final = `vaccine'_listing               if `vaccine'_listing       != .
	}

	summ     bcg_final penta1_final penta2_final penta3_final measles1_final

	egen     total_vaccines = rowtotal(bcg_final penta1_final penta2_final penta3_final measles1_final), m

	count if total_vaccines == 0

	
	drop  if total_vaccines == 0

	* append endline data for columns (2) and (3)
	append  using  "${Replicate_SocialSignals_dtaInter}/Endline_Survey_Data.dta"

	* merge the ANC treatment status:
	merge m:m clinic  using  "${Replicate_SocialSignals_dtaInter}/MasterData_PilotClinics.dta", gen(ANCtreat)


	* Label treatment variables:
	drop    treat?

	tab     intervention_arm, gen(treat)

	lab var treat3 "Signal at 4"
	lab var treat4 "Signal at 5"

	* Drop control:
	drop if intervention_arm == 1


	* Vaccine 4 or 5:
	gen     vaccine4 =.
	replace vaccine4 = 1        if penta3_listing  == 1|p3_endline == 1

	gen     vaccine5 =.
	replace vaccine5 = 1        if measles1_listing == 1|m1_endline == 1


	* Generate indicators on bracelet retention:
	replace wearing_brac_listing = .            if wearing_brac_listing==99 

	* not receive
	gen     notreceive = 1                      if brac_color==4
	replace notreceive = 0                      if inlist(brac_color,1,2)


	gen     lostbrac  = 1                        if brac_notwear_1==1|brac_notwear_2==1|brac_notwear_3==1|brac_notwear_4==1
	replace lostbrac  = 1                        if brac_still_have==0
	replace lostbrac  = 1                        if brac_show==0
	replace lostbrac  = 0                        if brac_show==1|brac_wearing==1

	gen     lostbrac2 = 1                        if brac_notwear_1==1
	replace lostbrac2 = 1                        if brac_still_have==0
	replace lostbrac2 = 1                        if brac_show==0
	replace lostbrac2 = 0                        if brac_show==1|brac_wearing==1

	bys     intervention_arm: summ    lostbrac lostbrac2

	ttest   lostbrac2 if inlist(intervention_arm,3,4), by(intervention_arm)


	* Bracelet wearing at endline:
	tab     brac_wearing
	rename  brac_wearing brac_wearing_endline

	* Bracelet exchanged yes /no
	tab     bracelet_exchanged
	replace bracelet_exchanged = 0              if bracelet_exchanged==99

	summ    wearing_brac_listing lostbrac bracelet_exchanged arm_anc

	save  "${Replicate_SocialSignals_dtaFinal}/ListingEndline_BraceletRetention_constructed.dta", replace


	* ------------------------------------------------------------------------------
	* Generate regression tables:


	use    "${Replicate_SocialSignals_dtaFinal}/ListingEndline_BraceletRetention_constructed.dta", clear


	** Demean ANC Variables:
	gen  outcome1 = wearing_brac_listing        if  age_listing <= 365
	gen  outcome2 = bracelet_exchanged          if (vaccine4==1|vaccine5==1) &  age_pikin <= 365
	* Full Sample: Endline
	gen  outcome3 = lostbrac                    if  age_pikin <= 365
	* Sample: 3.5 to 6.5 months
	gen  outcome4 = lostbrac                    if age_pikin  >= 107  & age_pikin <= 198
	* Sample: 9 to 12 months
	gen  outcome5 = lostbrac                    if age_pikin  >= 274  & age_pikin <= 365


	tab  arm_anc, gen(anc)

	foreach anc_treat of numlist 2,3,4 {
		foreach outcome of numlist 1(1)5 {
			summ  anc`anc_treat'  if intervention_arm==2   & outcome`outcome' !=.
			gen   anc`anc_treat'_dm`outcome' = anc`anc_treat' - `r(mean)'       if outcome`outcome' !=.
		}
	}

	summ   outcome1   anc2_dm1  anc3_dm1  anc4_dm1
	summ   outcome2   anc2_dm2  anc3_dm2  anc4_dm2
	summ   outcome3   anc2_dm3  anc3_dm3  anc4_dm3
	summ   outcome4   anc2_dm4  anc3_dm4  anc4_dm4
	summ   outcome5   anc2_dm5  anc3_dm5  anc4_dm5


	eststo clear
	foreach outcome of numlist 1(1)5 {
		areg     outcome`outcome'  treat3 treat4       anc2_dm`outcome'  anc3_dm`outcome'  anc4_dm`outcome', absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		estadd scalar C_mean        = _b[_cons]
		estadd local Obs            = "`e(N)'"
		eststo model`outcome'
	}

	esttab  ///
	model1 model2 model3  model4 model5       ///
	using "${Replicate_SocialSignals_ATables}/Table_BraceletRetentionAndCorrectHandOuts_raw.tex",  ///
	prehead("\begin{tabular}{l*{6}{c}} \toprule "                       ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{Child wears bracelet}} & \multicolumn{1}{c}{\textbf{Bracelet was exchanged}} & \multicolumn{3}{c}{\textbf{Child lost bracelet}}  \\"  ///
	"                                                 &                                                   &                                                     & \multicolumn{1}{c}{Full Sample}  & \multicolumn{1}{c}{3.5-6.5 months} & \multicolumn{1}{c}{9-12 months} \\")  ///
	scalars("C_mean         Uninformative Bracelet mean"                ///
	"Obs            Observations"                               ///
	"treat3_treat4  p(S4 = S5)")                                      ///
	keep( treat3 treat4 )                                               ///
	nomtitles                                                           ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	addnotes("") label se  nobaselevels noconstant noobs nolines        ///
	posthead("\midrule") postfoot("\bottomrule \end{tabular}") tex      ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_BraceletRetentionAndCorrectHandOuts_raw.tex"             ///
	"${Replicate_SocialSignals_ATables}/Table_BraceletRetentionAndCorrectHandOuts.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_BraceletRetentionAndCorrectHandOuts_raw.tex"


	* --------------------------------------------------------------------------
	* Outsheet p-values:
	* --------------------------------------------------------------------------


	esttab  ///
	model1 model2 model3  model4 model5       ///
	using "${Replicate_SocialSignals_ATables}/Table_BraceletRetentionAndCorrectHandOuts_raw.tex",  ///
	prehead("\begin{tabular}{l*{6}{c}} \toprule "                       ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{Child wears bracelet}} & \multicolumn{1}{c}{\textbf{Bracelet was exchanged}} & \multicolumn{3}{c}{\textbf{Child lost bracelet}}  \\"  ///
	"                                                 &                                                   &                                                     & \multicolumn{1}{c}{Full Sample}  & \multicolumn{1}{c}{3.5-6.5 months} & \multicolumn{1}{c}{9-12 months} \\")  ///
	scalars("C_mean         Uninformative Bracelet mean"                ///
	"Obs            Observations"                               ///
	"treat3_treat4  p(S4 = S5)")                                ///
	keep( treat3 treat4 )                                               ///
	nomtitles                                                           ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                                      ///
	addnotes("") label   nobaselevels noconstant noobs nolines          ///
	posthead("\midrule") postfoot("\bottomrule \end{tabular}") tex      ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_BraceletRetentionAndCorrectHandOuts_raw.tex"             ///
	"${Replicate_SocialSignals_ATables}/pvalues/Table_BraceletRetentionAndCorrectHandOuts_pvals.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_BraceletRetentionAndCorrectHandOuts_raw.tex"




* **************************************************************************** *
* End of Do file !!!
* **************************************************************************** *
